---@diagnostic disable
local modid = 'avatar_xi'

local T = Action({rmb=true,do_not_locomote=true,map_action=true})

T.id = 'ACTION_AVATAR_XI_HEATROCK_TP'
T.str = STRINGS.MOD_AVATAR_XI.ACTIONS.ACTION_AVATAR_XI_HEATROCK_TP
T.fn = function(act)
    if act == nil or act.doer == nil or act.pos == nil then
        return false
    end
    local _doer = act.doer
    ---@cast _doer ent
    local inventory = act.doer.components.inventory
    ---@cast inventory component_inventory
    if  not (inventory and inventory:HasItemWithTag('xi_heatrock',1) ) then
        return false
    end
    local inst = act.doer
    local targetpos = act.pos:GetPosition()
    local pt = inst:GetPosition()
    local offset = nil
    for i = 3, 7, 1 do
        offset = FindWalkableOffset(pt,math.random() * 2 * PI,i + math.random(),16,false,true,nil,true,true)
        if  offset then
            break
        end
    end
    if offset ~= nil then
        pt = pt + offset
    end
    local portal = SpawnPrefab('pocketwatch_portal_entrance')
    if portal then
        if portal and portal.components.teleporter then
            portal.components.teleporter.onActivate = function(_, doer)
                if  doer.components.talker ~= nil then
                    doer.components.talker:ShutUp()
                end
            end
        end
        portal.Transform:SetPosition(pt:Get())
        portal:SpawnExit(TheShard:GetShardId(), targetpos.x or pt.x, 0, targetpos.z or pt.z)
        inst.SoundEmitter:PlaySound('wanda1/wanda/portal_entrance_pre')
    end

    --*********
    SendModRPCToClient(GetClientModRPC(modid,'toggle_minimap'),inst.userid)
    return true
end

AddAction(T)

AddStategraphActionHandler('wilson',ActionHandler(ACTIONS.ACTION_AVATAR_XI_HEATROCK_TP))
AddStategraphActionHandler('wilson_client',ActionHandler(ACTIONS.ACTION_AVATAR_XI_HEATROCK_TP))

ACTIONS_MAP_REMAP[ACTIONS.ACTION_AVATAR_XI_HEATROCK_TP.code] = function (act,targetpos)
    local doer = act.doer
    local inventory = doer.replica.inventory
    ---@cast doer ent
    if inventory and inventory:HasItemWithTag('xi_heatrock',1) then
        return BufferedAction(doer,nil,ACTIONS.ACTION_AVATAR_XI_HEATROCK_TP,act.invobject,targetpos)
    end
end


AddComponentPostInit('playercontroller',
---comment
---@param self component_playercontroller
function (self)
    function self:avatar_xi_heatrock_tpUnicornRemapAction(act,positon,maptarget)
        local act_remap = nil
        if act and ACTIONS_MAP_REMAP[ACTIONS.ACTION_AVATAR_XI_HEATROCK_TP.code] then
            local px,py,pz = positon:Get()
            if self.inst:CanSeePointOnMiniMap(px,py,pz) then
                act_remap = ACTIONS_MAP_REMAP[ACTIONS.ACTION_AVATAR_XI_HEATROCK_TP.code](act,Vector3(px,py,pz))
            end
        end
        return act_remap
    end

    local oldGetMapActions = self.GetMapActions
    function self:GetMapActions(position,maptarget)
        local LMBaction,RMBaction = oldGetMapActions(self,position,maptarget)
        local inventory = self.inst.replica.inventory
        if inventory and inventory:HasItemWithTag('xi_heatrock',1) then
            local act = BufferedAction(self.inst,nil,ACTIONS.ACTION_AVATAR_XI_HEATROCK_TP)
            RMBaction = self:avatar_xi_heatrock_tpUnicornRemapAction(act,position,maptarget)
        end
        return LMBaction,RMBaction
    end

    local oldOnMapAction = self.OnMapAction
    function self:OnMapAction(actioncode,position,maptarget)
        local act = MOD_ACTIONS_BY_ACTION_CODE[ACTIONS.ACTION_AVATAR_XI_HEATROCK_TP.mod_name][actioncode]
        if act and act.map_action then
            if self.ismastersim then
                local LMBaction,RMBaction = self:GetMapActions(position,maptarget)
                if act.rmb then
                    if RMBaction then
                        self.locomotor:PushAction(RMBaction,true)
                    end
                else
                    if LMBaction then
                        self.locomotor:PushAction(LMBaction,true)
                    end
                end
            elseif self.locomotor == nil then
                SendRPCToServer(RPC.DoActionOnMap,actioncode,position.x,position.z)
            elseif self:CanLocomote() then
                local _, RMBaction = self:GetMapActions(position,maptarget)
                RMBaction.preview_cb = function ()
                    SendRPCToServer(RPC.DoActionOnMap,actioncode,position.x,position.z)
                end
                self.locomotor:PreviewAction(RMBaction,true)
            end
        end
        return oldOnMapAction(self,actioncode,position,maptarget)
    end
end)

AddClientModRPCHandler(modid,'toggle_minimap',function()
    if ThePlayer and ThePlayer.HUD and ThePlayer.HUD.controls then
        ThePlayer.HUD.controls:ToggleMap()
    end
end)